#include "../../config.h"

#include <glibmm/i18n.h>

#include "../application.h"
#include "p_migemo.h"
#include "p_macros.h"

namespace gpyp {
namespace pref {

//========================
// Migemo
void Migemo::update_option_sensitive() {
  cmigemo_  .set_sensitive(false);
  migemosrv_.set_sensitive(false);
  if (mode_cmigemo_.get_active()) {
    cmigemo_.set_sensitive(true);
  } else if (mode_migemosrv_.get_active()) {
    migemosrv_.set_sensitive(true);
  }
}
void Migemo::mode_disabled_on_toggled() {
  if (mode_disabled_.get_active()) {
    app_.conf().migemo()->mode() = MIGEMO_MODE_DISABLED;
    update_option_sensitive();
  }
}
void Migemo::mode_cmigemo_on_toggled() {
  if (mode_cmigemo_.get_active()) {
    app_.conf().migemo()->mode() = MIGEMO_MODE_CMIGEMO;
    update_option_sensitive();
  }
}
void Migemo::mode_migemosrv_on_toggled() {
  if (mode_migemosrv_.get_active()) {
    app_.conf().migemo()->mode() = MIGEMO_MODE_MIGEMOSRV;
    update_option_sensitive();
  }
}

void Migemo::migemo_mode_on_changed() {
  switch(*app_.conf().migemo()->mode()) {
  case MIGEMO_MODE_DISABLED:  mode_disabled_ .set_active(true); break;
  case MIGEMO_MODE_CMIGEMO:   mode_cmigemo_  .set_active(true); break;
  case MIGEMO_MODE_MIGEMOSRV: mode_migemosrv_.set_active(true); break;
  }
  update_option_sensitive();
}

void Migemo::open(Gtk::Entry &entry) {
  Gtk::FileChooserDialog dialog(parent_, "Open");
  dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
  dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
  if (dialog.run() == Gtk::RESPONSE_OK) {
    entry.set_text(dialog.get_filename());
  }
}

void Migemo::dict_open_on_clicked() {
  open(dict_);
}
void Migemo::dict_user_open_on_clicked() {
  open(dict_user_);
}

void Migemo::search_on_enable_migemo_changed() {
  test_.set_sensitive(Search::migemo_usable());
}
void Migemo::test_from_on_changed() {
  try {
    test_to_.set_text(Search::migemo_query(test_from_.get_text()));
  } catch(const Glib::ConvertError &ex) {
    test_to_.set_text(ex.what());
  } catch(loader::RefError err) {
    test_to_.set_text(err->get_message());
  } catch(...) {
    test_to_.set_text(_("Unknown error"));
  }
}

Migemo::Migemo(Application &app, Gtk::Window &parent) :
  Gtk::VBox      (false, 2),
  app_           (app),
  parent_        (parent),
  mode_disabled_ (rbg_mode_, _("Disable(*)")),
  mode_cmigemo_  (rbg_mode_, _("C/Migemo(*)")),
  mode_migemosrv_(rbg_mode_, _("Migemo server(*)")),
  cmigemo_       (_("C/Migemo(*)")),
  migemosrv_     (_("Migemo server")),
  test_          (_("Test"))
{
  Gtk::HBox *h1 = Gtk::manage(new Gtk::HBox(false, 2));
  h1->pack_start(mode_disabled_,  false, false);
  h1->pack_start(mode_cmigemo_,   false, false);
  h1->pack_start(mode_migemosrv_, false, false);
  pack_start(*h1, false, false);

  encoding_.append_text("EUC-JP");
  encoding_.append_text("UTF-8");
  encoding_.append_text("CP932");
  SETUP_SPINBUTTON(min_length_, 0, 100);

  Gtk::Table *t1 = Gtk::manage(new Gtk::Table());
  TABLE_LABEL(*t1, 0, _("Dictionary charset"));
  TABLE_LABEL(*t1, 1, _("Minimum query length"));
  t1->attach(encoding_,   1, 2, 0, 1);
  t1->attach(min_length_, 1, 2, 1, 2);
  TABLE_SPACING(*t1, 2, 2);
  pack_start(*t1, false, false);

#define CONN_BUTTON(B, N)                                               \
  (B).signal_clicked().connect(sigc::mem_fun(*this, &Migemo::N##_on_clicked))
  Gtk::Button *b_d = Gtk::manage(new Gtk::Button(Gtk::Stock::OPEN));
  Gtk::Button *b_u = Gtk::manage(new Gtk::Button(Gtk::Stock::OPEN));
  CONN_BUTTON(*b_d, dict_open);
  CONN_BUTTON(*b_u, dict_user_open);

  Gtk::Table *t2 = Gtk::manage(new Gtk::Table());
  TABLE_LABEL(*t2, 0, _("Dictionary"));
  TABLE_LABEL(*t2, 1, _("User dictionary"));
  t2->attach(dict_,      1, 2, 0, 1);
  t2->attach(*b_d,       2, 3, 0, 1, Gtk::SHRINK | Gtk::FILL);
  t2->attach(dict_user_, 1, 2, 1, 2);
  t2->attach(*b_u,       2, 3, 1, 2, Gtk::SHRINK | Gtk::FILL);
  TABLE_SPACING(*t2, 2, 2);
  t2->set_border_width(2);
  cmigemo_.add(*t2);
  pack_start(cmigemo_, false, false);

  Gtk::Table *t3 = Gtk::manage(new Gtk::Table());
  TABLE_LABEL(*t3, 0, _("URL"));
  t3->attach(url_, 1, 2, 0, 1);
  TABLE_SPACING(*t3, 2, 2);
  t3->set_border_width(2);
  migemosrv_.add(*t3);
  pack_start(migemosrv_, false, false);

  Gtk::Table *t4 = Gtk::manage(new Gtk::Table());
  t4->attach(test_from_, 0, 1, 0, 1);
  t4->attach(test_to_,   0, 1, 1, 2);
  TABLE_SPACING(*t4, 2, 2);
  t4->set_border_width(2);
  test_.add(*t4);
  pack_start(test_, false, false);

  pack_end(*Gtk::manage(new Gtk::Label(_("(*)Restart required"),
                                       Gtk::ALIGN_LEFT)), false, false);

  ConfMain::RefMigemo migemo = app_.conf().migemo();
  migemo_mode_on_changed();
  conn_mode_changed_ = migemo->mode()->signal_changed()
    .connect(sigc::mem_fun(*this, &Migemo::migemo_mode_on_changed));
  migemo->encoding()      ->attach(encoding_.get_entry()->property_text());
  migemo->min_length()    ->attach(min_length_.property_value());
  migemo->dict_path()     ->attach(dict_.property_text());
  migemo->dict_path_user()->attach(dict_user_.property_text());
  migemo->url()           ->attach(url_.property_text());

#define CONN_MODE(N)                                                    \
  mode_##N##_.signal_toggled()                                          \
    .connect(sigc::mem_fun(*this, &Migemo::mode_##N##_on_toggled))
  CONN_MODE(disabled);
  CONN_MODE(cmigemo);
  CONN_MODE(migemosrv);
#undef CONN_MODE

  test_from_.signal_changed()
    .connect(sigc::mem_fun(*this, &Migemo::test_from_on_changed));

  search_on_enable_migemo_changed();
  conn_enable_migemo_changed_ = Search::signal_enable_migemo_changed()
    .connect(sigc::mem_fun(*this, &Migemo::search_on_enable_migemo_changed));

#ifndef USE_MIGEMO
  mode_cmigemo_.set_sensitive(false);
  cmigemo_     .set_sensitive(false);
#endif
}

}/*pref*/
}/*gpyp*/
